package com.ssyt.ec.sys.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.sys.entity.MenuEntity;
import com.ssyt.ec.sys.service.SysMenuService;

@WebServlet("/SysMenuServlet")
public class SysMenuServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public SysMenuServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("listMenuop".equals(command)) {
			listMenuop(request, response);
		} else if ("listMenuclo".equals(command)) {
			listMenuclo(request, response);
		} else if ("addMenu".equals(command)) {
			addMenu(request, response);
		} else if ("updateState".equals(command)) {
			updateState(request, response);
		} else if ("getMenu".equals(command)) {
			getMenu(request, response);
		} else if ("updateMenu".equals(command)) {
			updateMenu(request, response);
		}
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 */
	private void updateMenu(HttpServletRequest request,
			HttpServletResponse response) {
		SysMenuService menuService = new SysMenuService();
		int id = Integer.parseInt(request.getParameter("id"));
		String menuName = request.getParameter("menuName");
		String url = request.getParameter("menuUrl");
		String state = request.getParameter("menuState");
		int menuState = 0;
		if ("open".equals(state)) {
			menuState = 1;
		} else {
			menuState = 2;
		}
		int data = menuService.updateMenu(id, menuName, url, menuState);
		print(response, FastjsonUtil.toJson(data));
	}

	/**
	 * 修改回显
	 * 
	 * @param request
	 * @param response
	 */
	private void getMenu(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		MenuEntity menu = DBUtil
				.getUniqueResult(
						MenuEntity.class,
						"SELECT a1.id,a1.pid,a1.text,a1.url,a1.state,a2.text as menutext FROM sys_menu a1,sys_menu a2 WHERE a1.id=? and a1.pid=a2.id",
						id);
		if (menu != null) {
			print(response, FastjsonUtil.toJson(menu));
		}
	}

	/**
	 * 更改菜单状态
	 * 
	 * @param request
	 * @param response
	 */
	private void updateState(HttpServletRequest request,
			HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		String value = request.getParameter("value");
		int value1 = 0;
		if ("closed".equals(value)) {
			value1 = 2;
		} else if ("open".equals(value)) {
			value1 = 1;
		}
		SysMenuService menuService = new SysMenuService();
		int data = menuService.updateState(id, value1);
		print(response, FastjsonUtil.toJson(data));
	}

	/**
	 * 菜单的新增
	 * 
	 * @param request
	 * @param response
	 */
	private void addMenu(HttpServletRequest request,
			HttpServletResponse response) {
		int pid = Integer.parseInt(request.getParameter("name"));
		String menuName = request.getParameter("menuName");
		String url = request.getParameter("menuUrl");
		int state = Integer.parseInt(request.getParameter("menuState"));
		SysMenuService menuService = new SysMenuService();
		int data = menuService.addMenu(pid, menuName, url, state);
		print(response, FastjsonUtil.toJson(data));
	}

	/**
	 * 显示禁用菜单
	 * 
	 * @param request
	 * @param response
	 */
	private void listMenuclo(HttpServletRequest request,
			HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		SysMenuService menuService = new SysMenuService();
		PageModel<MenuEntity> listMenu = menuService.listMenuclo(page, rows);
		print(response,
				FastjsonUtil.toJsonWithDateFormat(listMenu, "yyyy-MM-dd"));
	}

	/**
	 * 显示启用菜单
	 * 
	 * @param request
	 * @param response
	 */
	private void listMenuop(HttpServletRequest request,
			HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		SysMenuService menuService = new SysMenuService();
		PageModel<MenuEntity> listMenu = menuService.listMenuop(page, rows);
		print(response,
				FastjsonUtil.toJsonWithDateFormat(listMenu, "yyyy-MM-dd"));
	}

}
